package com.ayuku.course.ui.coursecategory;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableItem;

import com.ayuku.commons.AYConst;
import com.ayuku.commons.SqliteDB;
import com.ayuku.commons.Util;
import com.ayuku.commons.ui.ListWithFormMaster;
import com.ayuku.course.persistence.model.Category;
import com.ayuku.course.persistence.model.CourseLevel;

public class ListCategory extends ListWithFormMaster {
	private CompCategory comp;
	private Category pjo;
	private String name;
	private String kode;

	public ListCategory(Composite parent, int style) {
		super(parent, style);
	}

	@Override
	protected String[] getColumCaption() {
		final String[] titles = { "Kode", "Nama" };
		return titles;
	}

	@Override
	protected int[] getColumWidth() {
		int[] i = { 80, 300 };
		return i;
	}

	@Override
	protected void setFormKeyWithSelectedTableItem() {
		TableItem[] selection = getTableData().getSelection();
		if ((selection != null) && (selection.length > 0)) {
			TableItem itm = (TableItem) selection[0];
			setFormKey(itm.getText(0));
		} else {
			Util.showMsgBox(AYConst.FAIL_VIEW, AYConst.MSG_WARN, getShell());
		}
	}

	@Override
	public void additionalProcess() {
		//put here code for form
	}

	public static List<CourseLevel> getList(String arg) throws Exception {
		return SqliteDB
				.getPersist()
				.readList(
						CourseLevel.class,
						"SELECT * FROM CATEGORY WHERE code LIKE ? or name LIKE ?",
						new Object[] { arg, arg });
	}

	@Override
	protected void populateTable(String sqlDml) throws Exception {
		getTableData().removeAll();
		List<CourseLevel> lst = getList(sqlDml);
		getTableData().setItemCount(lst.size());
		setData(new Object[lst.size()][]);

		for (int i = 0; i < lst.size(); i++) {
			CourseLevel pj = lst.get(i);
			getData()[i] = new String[] { Util.nullToString(pj.getCode()),
					Util.nullToString(pj.getName()) };
		}
	}

	@Override
	protected void fillTable(Object[] obj, TableItem item) {
		item.setText(0, Util.nullToString(obj[0]));
		item.setText(1, Util.nullToString(obj[1]));
	}

	@Override
	public void getCompositeForm(Composite formContainer) {
		comp = new CompCategory(formContainer, SWT.BORDER);

	}

	@Override
	public void reloadForm() {
		if (pjo != null) {
			comp.getTxtKode().setText(Util.nullToString(pjo.getCode()));
			comp.getTxtName().setText(Util.nullToString(pjo.getName()));
		}
	}

	@Override
	public void doClearForm(SelectionEvent arg0) {
		// reset form and empty all data
		pjo = new Category();
		// end of
		reloadForm();
	}

	@Override
	protected void disabledOtherButton(boolean enable) {

	}

	@Override
	protected void enabledControl(boolean otherfield, boolean key) {
		comp.getTxtKode().setEditable(key);
		comp.getTxtName().setEditable(otherfield);
	}

	@Override
	public boolean doValidate() {

		kode = comp.getTxtKode().getText();
		if (Util.isEmpty(kode)) {
			Util.showMsgBox("Kode harus dilengkapi!", 1, getShell());
			comp.getTxtKode().setFocus();
			return false;
		}

		name = comp.getTxtName().getText();
		if (Util.isEmpty(name)) {
			Util.showMsgBox("nama harus dilengkapi!", 1, getShell());
			comp.getTxtName().setFocus();
			return false;
		}

		return true;
	}

	@Override
	protected void setPojoWithFormField() throws Exception {
		if (pjo == null) {
			pjo = new Category();
		}
		pjo.setCode(kode);
		pjo.setName(name);
	}

	@Override
	protected void getPojoByFormKey(String key) throws Exception {
		try {
			pjo = (Category) SqliteDB.getPersist().readByPrimaryKey(
					Category.class, new Object[] { getFormKey() });
		} catch (Exception e) {

		}
		reloadForm();
	}
 
	@Override
	protected int doMainInsert() throws Exception {
		setPojoWithFormField();
		return SqliteDB.getPersist().insert(pjo);
	}

	@Override
	protected int doMainUpdate() throws Exception {
		setPojoWithFormField();
		return SqliteDB.getPersist().update(pjo);
	}

	@Override
	protected int doMainDelete() throws Exception {
		return delete(pjo.getCode());
	}

	@Override
	protected int delete(String key) throws Exception {
		return SqliteDB.getPersist().executeUpdate(
				"delete from category where code = ?",
				new Object[] { key.trim() });
	}

	@Override
	public void setFocusForAdd() {
		comp.getTxtKode().setFocus();		
	}

	@Override
	public void setFocusForEdit() {
		comp.getTxtName().setFocus();			
	}

}
